//index.js
//获取应用实例
var http = require('../../../utils/http.js')
const app = getApp()
Page({
  data: {
    isLogin: false,
    todayOrderMoney: '--',
    todayOrder: '--',
    onlinedDevice: '--',
    faultCount: '--',
    companyName: '--',
    permission: [],
    hasUserInfo: false,
    finish: false,
  },

  onLoad: function() {
    wx.hideTabBar({})
    var that = this
    this.setData({
      screenHeight: app.globalData.screenHeight,
      screenWidth: app.globalData.screenWidth,
    })
//     wx.getStorage({
//       key: 'token',
//       success: function(res) {
//         var token = res.data;
// 				app.globalData.token = token
// 				app.globalData.isLogin = true
// 				that.setData({
// 				  isLogin: true
// 				})
// 				if (app.globalData.isLogin) {
// 				  wx.showTabBar({
// 				
// 				  })
// 				}
// 				
// 				setTimeout(function() {
// 				  if (app.globalData.hasUserInfo) {
// 				    wx.showTabBar({
// 				
// 				    })
// 				  }
// 				}, 1000)
// 				if (app.globalData.hasUserInfo) {
// 				
// 				  wx.showTabBar({
// 				
// 				  })
// 				}
//         var baseUrl = app.globalData.baseUrl
//         wx.request({
//           url: baseUrl + 'devices/pagedSearch?page=0&size=10',
//           header: {
//             'AUTHORIZATION': token
//           },
//           success(res) {
//             if (res.data.code != 60006) {
// 
//             }
//           }
//         })

//       },
//       fail() {
//         wx.hideTabBar({})
//       }
//     })

//     if (app.globalData.userInfo) {
//       wx.setStorage({
//         key: 'userInfo',
//         data: app.globalData.userInfo,
//       })
//       this.setData({
//         userInfo: app.globalData.userInfo,
//         hasUserInfo: true
//       })
//     }
  },

  // 获取用户信息
  getUserInfo: function(e) {
		//console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    if (app.globalData.userInfo) {
      wx.setStorage({
        key: 'userInfo',
        data: app.globalData.userInfo,
      })
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      if (this.data.isLogin) {
        wx.showTabBar({

        })
      }
    } else {
      wx.hideTabBar({

      })
      this.setData({
        userInfo: null,
        hasUserInfo: false,
        isLogin: false
      })
    }


  },

  // 订单记录
  order_rec: function() {
    wx.navigateTo({
      url: '../order/order/order',
    })
  },

  // 设备列表
  device_paged_search: function() {
    wx.navigateTo({
      url: '../../menu/deviceList/deviceList?online=false',
    })
  },
  //在线设备
  device_online:function(){
    wx.navigateTo({
      url: '../../menu/deviceList/deviceList?online=true',
    })
  },

  // 点位管理
  location_con: function() {
    wx.navigateTo({
      url: '../../menu/region/region',
    })
  },

  // 预分配设备
  pre_assign_task_create: function() {
    wx.navigateTo({
      url: '/pages/menu/pre-assign/pre-assign',
    })
  },
  //故障工单
  fault_con: function() {
    wx.navigateTo({
      url: '/pages/menu/fault/fault-list',
    })
  },
  // 数据魔方
  dataMagic_module: function() {
    wx.navigateTo({
      url: "/pages/menu/data-magic/data-magic",
    })
  },

  // 设备补料
  add_met_menu: function() {
    wx.scanCode({
      success(res) {
        var length = res.result.length
        var qrId = ""
        var type = ""
        for (var i = 0; i < length; i++) {
          if (res.result[length - i - 1] == "=") {
            if (res.result[length - i - 4] == "e") {
              type = "DEVICE"
            } else if (res.result[length - i - 4] == "r") {
              type = "QR"
            }
            break;
          }
          qrId = res.result[length - i - 1] + qrId
        }
        wx.navigateTo({
          url: '../../menu/add-material/add-material?qrId=' + qrId + '&type=' + type,
        })
      },
      fail() {}
    })
  },
  // 登录
  login: function(res) {
		//console.log(res)
    var username = res.detail.value.username;
    var password = res.detail.value.password;
    if (username == "") {
      wx.showModal({
        title: '登录失败',
        content: '用户名不能为空！！',
      })
    } else if (password == "") {
      wx.showModal({
        title: '登录失败',
        content: '密码不能为空！！',
      })
    } else {
      var that = this;
			var data = {
				username:username,
				password:password
			};
      http.request('loginForMiniProgram',data,'form','post','',that.loginSuccess,(err)=>{
				console.log(err)
			})
    }
  },
	loginSuccess:function(res){
		let that = this;
		if(res.code==200){
			app.globalData.userId = res.data.userId;
			app.globalData.token = res.data.accessTokenVo.token_type + " " + res.data.accessTokenVo.access_token;
			app.globalData.companyName = res.data.companyName;
			app.globalData.companyId = res.data.companyId;
			app.globalData.userName = res.data.username;
			app.globalData.isLogin = true;
			that.setData({
				isLogin: app.globalData.isLogin,
				companyName: app.globalData.companyName,
				userName: app.globalData.userName
			});
			that.getStats();
			that.getDStats();
			that.getPermission();
			wx.showTabBar({});
			wx.setStorage({
				key: 'token',
				data: app.globalData.token,
			})
			wx.setStorage({
				key: 'userId',
				data: app.globalData.userId,
			})
			wx.setStorage({
				key: 'companyName',
				data: app.globalData.companyName,
			})
			wx.setStorage({
				key: 'userName',
				data: app.globalData.userName,
			})
		}else{
			wx.hideTabBar({})
			wx.showModal({
				title: '登录失败',
				content: res.data.message + '！！',
			})
		}
	},
  getStats: function(res) {
    var thm = this;
		http.request('statistics/orderStatistics','','json','get','',(res)=>{
			if (res.code == 200) {				
				thm.setData({
					todayOrderMoney: (res.data.todayOrderMoney).toFixed(2),
					todayOrder: res.data.todayOrder					
				})
			}
		},(err)=>{
			console.log(err)
		});

  },
  getDStats: function(res) {
    var thm = this;
		http.request('statistics/deviceStatistics','','json','get','',(res)=>{
			if (res.code == 200) {
				thm.setData({
					onlinedDevice: res.data.onlinedDevice,
					faultCount: res.data.faultCount ? res.data.faultCount : '--'
				})
			}
		},(err)=>{
			console.log(err)
		});	

  },
  // 页面加载
  onShow: function() {
    var that = this
    wx.getStorage({
      key: 'token',
      success: function(res) {
				//console.log(res)
        var token = res.data
        app.globalData.token = token; 
				app.globalData.isLogin = true;
				that.setData({
					isLogin:true
				})
				wx.showTabBar({});
        that.getStats();
        that.getDStats();
      },
      fail() {
        wx.hideTabBar({})
      }
    })
    wx.getStorage({
      key: 'userId',
      success: function(res) {
        app.globalData.userId = res.data
        that.getPermission();
      },
      fail() {
        // wx.hideTabBar({})
      }
    })
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        app.globalData.userInfo = res.data
        that.setData({
          hasUserInfo: true,
					userInfo:app.globalData.userInfo
        })
				wx.showTabBar({})
      },
      fail() {}
    })
    wx.getStorage({
      key: 'companyName',
      success: function(res) {
        app.globalData.companyName = res.data
        that.setData({
          companyName: res.data
        })
      },
      fail() {}
    })

    wx.getStorage({
      key: 'userName',
      success: function(res) {
        app.globalData.userName = res.data
        that.setData({
          userName: res.data
        })
      },
      fail() {}
    })

    that.setData({
      finish: true
    })
  },
  getPermission: function() {
    var userId = app.globalData.userId;
    var that = this;
		http.request('permission/'+userId,'','json','get','',that.permissinSuccess,(err)=>{
			console.log(err);
		});   
  },
	permissinSuccess:function(res){
		var that = this;
		if(res.code==200){
			var dataList = res.data;
			var permissiondata = [];
			var j;
			var k;
			for (var i = 0; i < dataList.length; i++) {
			  // 是否有小程序管理权限
			  if (dataList[i].permissionKey == "applet_module") {
			    for (j = 0; j < dataList[i].child.length; j++) {
			      // 是否有订单记录权限
			      if (dataList[i].child[j].permissionKey == "order_rec") {
			        dataList[i].child[j].permissionName = "订单记录";
			        permissiondata.push(dataList[i].child[j])
			      }
			      if (dataList[i].child[j].permissionKey == 'msg_notice') {
			        // console.log(123)
			        app.globalData.notifyAuth = true;
			      }
			    }
			  }
			
			  // 是否有数据魔方权限
			  if (dataList[i].permissionKey == "dataMagic_module") {
			    permissiondata.push(dataList[i])
			  }			
			
			  // 是否有点位管理权限
			  if (dataList[i].permissionKey == "location_module") {
			    for (j = 0; j < dataList[i].child.length; j++) {
			      // 是否有点位列表权限
			      if (dataList[i].child[j].permissionKey == "location_con") {
			        dataList[i].child[j].permissionName = "点位管理"
			        permissiondata.push(dataList[i].child[j])
			      }
			    }
			  }
			
			  // 是否有下级管理权限
			  if (dataList[i].permissionKey == "seller_module") {
			    for (j = 0; j < dataList[i].child.length; j++) {
			      // 是否有下级账号权限
			      if (dataList[i].child[j].permissionKey == "low_user_con") {
			        for (k = 0; k < dataList[i].child[j].child.length; k++) {
			          // 是否有预分配设备权限
			          if (dataList[i].child[j].child[k].permissionKey == "pre_assign_task_create") {
			            dataList[i].child[j].child[k].permissionName = "分配设备";
			            permissiondata.push(dataList[i].child[j].child[k])
			          }
			        }
			      }
			    }
			  }
			
			  // 是否有维保管理权限
			  if (dataList[i].permissionKey == "maintenance_module") {
			    for (j = 0; j < dataList[i].child.length; j++) {
			      // 是否有故障工单列表权限
			      if (dataList[i].child[j].permissionKey == "fault_con") {
			        dataList[i].child[j].permissionName = "故障工单"
			        permissiondata.push(dataList[i].child[j])
			      }
			    }
			  }
			
			  // 是否有设备管理权限
			  if (dataList[i].permissionKey == "device_module") {
			    for (j = 0; j < dataList[i].child.length; j++) {
			      // 是否有设备列表权限
			      if (dataList[i].child[j].permissionKey == "device_paged_search") {
			        permissiondata.push(dataList[i].child[j])
			      }
			    }
			  }
			
			
			}
			var permission = [];
			for (i = 0; i < permissiondata.length; i++) {
			  if (permissiondata[i].permissionName == "订单记录") {
			    permission.push(permissiondata[i]);
			    break;
			  }
			}
			for (i = 0; i < permissiondata.length; i++) {
			  if (permissiondata[i].permissionName == "数据魔方") {
			    permission.push(permissiondata[i]);
			    break;
			  }
			}
			for (i = 0; i < permissiondata.length; i++) {
			  if (permissiondata[i].permissionName == "点位管理") {
			    permission.push(permissiondata[i]);
			    break;
			  }
			}
			for (i = 0; i < permissiondata.length; i++) {
			  if (permissiondata[i].permissionName == "设备列表") {
			    permission.push(permissiondata[i]);
			    break;
			  }
			}
			for (i = 0; i < permissiondata.length; i++) {
			  if (permissiondata[i].permissionName == "故障工单") {
			    permission.push(permissiondata[i]);
			    break;
			  }
			}
			for (i = 0; i < permissiondata.length; i++) {
			  if (permissiondata[i].permissionName == "分配设备") {
			    permission.push(permissiondata[i]);
			    break;
			  }
			}
			that.setData({
			  permission: permission,
			  permissionList: dataList
			})
			that.getPermission1();
		}else{
			console.log(res.data.message);
		}
		
	},
  // 幸云获取用户部分权限
  getPermission1: function() {
    var permissionList = this.data.permissionList
    //console.log(permissionList)
    for (var i = 0; i < permissionList.length; i++) {
      if (permissionList[i].permissionKey == "finance_module") {
        var child = permissionList[i].child;
        for (var a = 0; a < child.length; a++) {
          if (child[a].permissionKey == "personAcc_con") {
            app.globalData.personAcc_con = true;
            break;
          }
        }
      }
      if (permissionList[i].permissionKey == "applet_module") {
        var child = permissionList[i].child;
        for (var a = 0; a < child.length; a++) {
          if (child[a].permissionKey == "applet_clock_in_record") {
            app.globalData.clockIn = true;
            break;
          }
        }

      }
    }
  },



})